/*
 * Copyright (C) 2008
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.sca4idea.references;

import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.ElementManipulator;
import com.intellij.psi.impl.source.resolve.reference.impl.providers.GenericReferenceProvider;
import com.intellij.psi.impl.source.resolve.reference.impl.CachingReference;
import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NotNull;

/**
 * Base class inherited by all F3 reference providers. 
 */
public abstract class AbstractReferenceProvider extends GenericReferenceProvider {
    @NotNull
    public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {
        String text = psiElement.getText();
        ElementManipulator elementmanipulator = CachingReference.getManipulator(psiElement);
        
        if (elementmanipulator == null) {
            return getReferencesByString(text, psiElement,  0);
        } else {
            TextRange textrange = elementmanipulator.getRangeInElement(psiElement);
            String s1 = text.substring(textrange.getStartOffset(), textrange.getEndOffset());
            return getReferencesByString(s1, psiElement, textrange.getStartOffset());
        }
    }

    @NotNull
    public abstract PsiReference[] getReferencesByString(String s, PsiElement psiElement, int i);
}
